
import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    count: 0,
    num: 2
  },
  mutations: {
    increment(state) {
      state.count++;
    },
    decrement(state) {
      state.count--;
    },
    // 第二个参数就是传过来的参数 是一个对象
    incrementN(state, payload) {
      state.count += payload.n;
    },
  },
  actions: {
    // 异步 传入一个阉割版的store 然后调用commit方法
    incrementWait(store) {
      setTimeout(() => {
        store.commit("increment");
      }, 2000);
    },

    incrementWaitN(store, n) {
      setTimeout(() => {
        store.commit("incrementN", { n: n });
      }, 2000);
    }
  },
  getters: {}
});